﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class SystemController : BaseController
    {
        //
        // GET: /Admin/System/

        public ActionResult Index()
        {
            SystemModel model = new SystemModel();
            return View();
        }
        [HttpPost]
        public ActionResult Index(SystemModel model)
        {
            HttpContext.Session["SkinName"] = model.SkinName;
            return View(model);
        }

        public ActionResult Template()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AjaxGetTemplate(int pageIndex, int pageSize)
        {
            List<object> tempList = new List<object>();
            string path = Server.MapPath("~/Templates");
            string[] files = Directory.GetFiles(path);
            foreach (var item in files)
            {
                FileInfo f = new FileInfo(item);
                tempList.Add(new { TempFileName = f.Name });
            }
            return Content(SerializeJson(new { results = 2, rows = tempList }));
        }

        public ActionResult EditTemp(string tempUrl)
        {
            TemplateModel model = new TemplateModel();
            string path = Server.MapPath("~/Templates/" + tempUrl);
            //FileStream fs =System.IO.File.OpenRead(path);
            //byte[] body = new byte[fs.Length];
            //fs.Read(body, 0, body.Length);
            StreamReader sr = new StreamReader(path, Encoding.Default);
            model.TempName = tempUrl;
            model.TempBody = sr.ReadToEnd();
            return View(model);
        }

        [HttpPost]
        public ActionResult EditTemp(TemplateModel model)
        {
            if (ModelState.IsValid)
            {
                model.Update();
                return Content("<script>SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }

        public ActionResult SysMsg()
        {
            MsgModel model = new MsgModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult AjaxGetMsgList(MsgModel model)
        {
            var data = model.GetSysMsgList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        [HttpPost]
        public ActionResult DeleteMsg()
        {
            string idStrList = Request["ids[]"];
            MsgModel model = new MsgModel();
            model.Delete(idStrList);
            return Content(SerializeJson(new { result = true, msg = "删除成功！" }));
        }

        public ActionResult Detail(string id)
        {
            ViewBag.UserName = SessionEnt.UserName;
            MsgModel model = new MsgModel();
            var data = model.GetSysMsgByParentID(id);
            return View(data);
        }

        public ActionResult Database()
        {
            return View();
        }

        public ActionResult SysLog()
        {
            SysLogModel model = new SysLogModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult AjaxGetSysLog(SysLogModel model)
        {
            var data = model.GetSysLogList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        public ActionResult DeleteLog()
        {
            string idStrList = Request["ids[]"];
            SysLogModel model = new SysLogModel();
            model.Delete(idStrList);
            return Content(SerializeJson(new { result = true, msg = "删除成功！" }));
        }

        public ActionResult Traffic()
        {
            TrafficModel model = new TrafficModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult AjaxGetTraffic(TrafficModel model)
        {
            var data = model.GetTrafficStatistics();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        [HttpPost]
        public ActionResult TrafficClear()
        {
            string dateStrList = Request["dates[]"];
            TrafficModel model = new TrafficModel();
            model.Clear(dateStrList);
            return Content(SerializeJson(new { result = true, msg = "清除成功！" }));
        }

        public ActionResult TrafficDetail(DateTime date)
        {
            TrafficModel model = new TrafficModel();
            model.Date = date;
            return View(model);
        }

        [HttpPost]
        public ActionResult AjaxGetTrafficInfo(TrafficModel model)
        {
            var data = model.GetTrafficInfoList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        [HttpPost]
        public ActionResult DeleteTraffic()
        {
            string dateStrList = Request["ids[]"];
            TrafficModel model = new TrafficModel();
            model.Delete(dateStrList);
            return Content(SerializeJson(new { result = true, msg = "删除成功！" }));
        }

        public ActionResult AdManage()
        {
            AdModel model = new AdModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult AjaxGetAdList(AdModel model)
        {
            var data = model.GetList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        public ActionResult AddAd()
        {
            AdModel model = new AdModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult AddAd(AdModel model)
        {
            if (ModelState.IsValid)
            {
                model.Add();
                return Content("<script>SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult EditAd(string id)
        {
            AdModel model = new AdModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult EditAd(AdModel model)
        {
            if (ModelState.IsValid)
            {
                model.Edit();
                return Content("<script>SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult DeleteAd(string id)
        {
            return Content("true");
        }

    }
}
